package com.jay.example.IndoorPositioningSystem;

/****
 * https://gitee.com/unw1900/Indoor_Positioning_System
 */
class MovingAverage {

    private static final int WINDOWS = 10;
    private double[] mTemp = null; // 只声明暂时不初始化,用来记录最后得不到均值处理的点
    private double[] mBufout = null;
    private int mWindowSize = WINDOWS;	//窗口大小
    private double[] accbuf = new double[mWindowSize];
    private float[] degreebuf = new float[mWindowSize];
    private int pointer = 0;//序列指针
//    private int accnum = 0;
//    double[] accAverage = new double[10];
//    private short[] accout = null;

    /*******************
     * 滑动滤波：将输入序列右移 1 位，先进先出，对序列求平均值，序列长度为：WINDOWS
     * @return 窗口内数据平均后输出
     */
    float movingAverageFilter(float data) {
        float outData;
        if(pointer < mWindowSize) {
            degreebuf[pointer++] = data;
        } else {
            if (degreebuf.length - 1 >= 0)
                System.arraycopy(degreebuf, 1, degreebuf, 0, degreebuf.length - 1);//数组右移1位
            degreebuf[degreebuf.length-1] = data;//最后一位补新数据
        }
        outData = average(degreebuf);
        return outData;
    }

    /*******************
     * 滑动滤波：将输入序列右移 1 位，先进先出，对序列求平均值，序列长度为：WINDOWS
     * @param data 最新数据
     * @return 窗口内数据平均后输出
     */
    double movingAverageFilter(double data) {
        double outData;
        if(pointer < mWindowSize) {
            accbuf[pointer++] = data;
        } else {
            if (accbuf.length - 1 >= 0)
                System.arraycopy(accbuf, 1, accbuf, 0, accbuf.length - 1);//数组右移1位
            accbuf[accbuf.length-1] = data;//最后一位补新数据
        }
        outData = average(accbuf);
        return outData;
    }

    /****************
     * 计算输入序列的平均值，并输出
     * @param array (float)输入序列
     * @return 输出计算后的平均值
     */
    float average(float[] array) {
        float sum = 0;
        float output;
        for (double v : array) {
            sum += v;
        }
        output = (sum / array.length);
        return output;
    }

    /****************
     * 计算输入序列的平均值，并输出
     * @param array (double)输入序列
     * @return 输出计算后的平均值
     */
    private double average(double[] array) {	//求均值
        double sum = 0;
        double output;
        for (double v : array) {
            sum += v;
        }
        output = (sum / array.length);
        return output;
    }




}
